﻿using OxyPlot;
using OxyPlot.Wpf;
using System.Windows;
using System.Windows.Media;
namespace CoreModule
{
    /// <summary>
    /// 扩展
    /// </summary>
    public static class Extension
    {
        /// <summary>
        /// 
        /// </summary>
        public static float MaxValidValue = 35.0f;

        /// <summary>
        /// 获取 时间
        /// </summary>
        /// <param name="queryDay"></param>
        /// <returns></returns>
        public  static DateTime GetBaseDateTime(QueryDay queryDay)
        {
            var now = DateTime.Now;

            switch (queryDay)
            {
                case QueryDay.None:
                    return now.AddDays(-1000);
                case QueryDay.Day7:
                    return now.AddDays(1 - 7).Date;
                case QueryDay.Day30:
                    return now.AddDays(1 - 30).Date;
                case QueryDay.Day90:
                    return now.AddDays(1-90).Date;
                default:
                    return now.AddDays(-1000);
            }



        }

        /// <summary>
        /// 蓝牙数据 超时秒数
        /// </summary>
        /// <returns></returns>
        public static int GetBluetoothDataTimeoutSeconds()
        {
            return 5;
        }
        /// <summary>
        /// 获取 测试数据的序列号
        /// </summary>
        /// <param name="first"></param>
        /// <param name="second"></param>
        /// <returns></returns>
        public static int GetSeqNo(byte first,byte second)
        {
            return first + second * 256;
        }

        /// <summary>
        /// 图表 线的宽度
        /// </summary>
        /// <returns></returns>
        public static double GetLineSeriesStrokeThickness()
        {
            return 1.0;
        }

        /// <summary>
        /// 图表 线的颜色
        /// </summary>
        /// <returns></returns>
        public static OxyColor GetLineSeriesColor()
        {
            return OxyColor.FromRgb(0xBC,0xBF,0xCC );
        }

        /// <summary>
        /// 图表 点注释的 点的大小
        /// </summary>
        /// <returns></returns>
        public static int GetPointAnnotationSize()
        {
            return 3;
        }
        /// <summary>
        /// 图表 测量范围 的背景色
        /// </summary>
        /// <returns></returns>
        public static OxyColor GetRectangleAnnotationOxyColor()
        {
            return OxyColor.FromRgb(0xF0, 0xF1, 0xF5);
        }

        /// <summary>
        /// 上下限制线的颜色
        /// </summary>
        /// <returns></returns>
        public static OxyColor GetLimitLineOxyColor()
        {
            return OxyColor.FromRgb(0xE2, 0xE3, 0xEB); ;
        } 

        /// <summary>
        /// 血糖状态 的颜色
        /// </summary>
        /// <param name="bloodGlucoseStatus"></param>
        /// <returns></returns>

        public static OxyColor GetBloodGlucoseStatusOxyColor(BloodGlucoseStatus bloodGlucoseStatus)
        {
            return GetBloodGlucoseStatusColor(bloodGlucoseStatus).ToOxyColor();
        }

        /// <summary>
        /// 血糖状态 的颜色
        /// </summary>
        /// <param name="bloodGlucosePeriod"></param>
        /// <param name="measurementValue"></param>
        /// <returns></returns>
        public static Color GetBloodGlucoseStatusColor(BloodGlucoseStatus  bloodGlucoseStatus)
        {
            switch (bloodGlucoseStatus)
            {
                case BloodGlucoseStatus.None:
                    return Color.FromRgb(0, 0, 0);
                case BloodGlucoseStatus.LO:
                    return Color.FromRgb(0, 0, 0);
                case BloodGlucoseStatus.Other:
                    return Color.FromRgb(0, 0, 0);
                case BloodGlucoseStatus.High:
                    return Color.FromRgb(0xF5,0x8B,0x00);
                case BloodGlucoseStatus.Low:
                    return Color.FromRgb(0xEB,0x1C,0x2A);
                case BloodGlucoseStatus.Normal:
                    return Color.FromRgb(0x11,0xA8,0x4D);
                case BloodGlucoseStatus.HI:
                    return Color.FromRgb(0, 0, 0);
                default:
                    return Color.FromRgb(0x11, 0xA8, 0x4D);
            }
        }

        /// <summary>
        ///  由于设备没有提供时段，故使用时间来计算
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static BloodGlucosePeriod GetBloodGlucosePeriod(DateTime  dateTime)
        {
            return BloodGlucosePeriod.Random;
        }

        /// <summary>
        /// 时段和测量值 计算血糖状态
        /// </summary>
        /// <param name="bloodGlucosePeriod"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static BloodGlucoseStatus GetBloodGlucoseStatus(BloodGlucosePeriod bloodGlucosePeriod ,double measurementValue)
        {

            if (measurementValue < 0.6)
            {
                return BloodGlucoseStatus.LO;
            }
            else if (measurementValue > 33.3)
            {
                return BloodGlucoseStatus.HI;
            }
            //11.1
            else if (measurementValue >= CoreConstant.MinHighValue)
            {
                return BloodGlucoseStatus.High;
            }
            ///3.9
            else if (measurementValue >=CoreConstant.MinNormalValue)
            {
                return BloodGlucoseStatus.Normal;
            }
            
            else
            {
                return BloodGlucoseStatus.Low;
            }
        }

        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>

        public static Window GetWindow(this FrameworkElement element)
        {
            // 获取可视树中的上一级
            DependencyObject parent = VisualTreeHelper.GetParent(element);

            // 如果父级是null，则直接返回null
            if (parent == null) return null;

            // 如果父级已经是Window，则直接返回
            if (parent is Window window)
                return window;
            else
                // 否则递归查找
                return GetWindow((FrameworkElement)parent);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <param name="typename"></param>
        /// <returns></returns>
        public static List<T> GetChildObjects<T>(DependencyObject obj, Type typename, bool recursion = true) where T : FrameworkElement
        {


            DependencyObject child = null;
            List<T> childList = new List<T>();

            for (int i = 0; i <= VisualTreeHelper.GetChildrenCount(obj) - 1; i++)
            {
                child = VisualTreeHelper.GetChild(obj, i);

                if (child is T && (((T)child).GetType() == typename))
                {
                    childList.Add((T)child);
                }
                if (recursion)
                {
                    childList.AddRange(GetChildObjects<T>(child, typename));
                }
            }
            return childList;
        }
       
    }
}
